package com.magicalcoder.pigeon.mysql.manager.orm;

import org.objenesis.Objenesis;
import org.objenesis.ObjenesisStd;

import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hzhedongyu on 2017/6/12.
 * 映射 把数据库字段映射成java
 */
public class ORMap<T> {

    private static Objenesis objenesis = new ObjenesisStd(true);

    public List<T> coverToList(ResultSet resultSet,Class<T> clazz) throws SQLException, IllegalAccessException {
        List<T> beanList = new ArrayList<T>();
            while (resultSet.next())
            {
                Object bean = objenesis.newInstance(clazz);
                Field[] fields = bean.getClass().getDeclaredFields();
                if(fields!=null){
                    for(Field field:fields){
                        field.setAccessible(true);
                        String fieldName = field.getName();
                        field.set(bean,resultSet.getObject(fieldName));
                    }
                }
                beanList.add((T)bean);
            }
        return beanList;
    }

}
